package com.free.file.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.ftp.Ftp;
import com.free.common.core.constant.FileFtpConstants;
import com.free.common.core.pojo.FileInfo;
import com.free.common.core.utils.NumberUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

/**
 * 本地文件存储
 *
 * @author free
 */
@Primary
@Service
@Slf4j
public class LocalSysFileServiceImpl implements ISysFileService {
    /**
     * 资源映射路径 前缀
     */
    @Value("${file.prefix}" )
    public String localFilePrefix;

    /**
     * 域名或本机访问地址
     */
    @Value("${file.domain}" )
    public String domain;

    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.path}" )
    private String localFilePath;

    /**
     * 本地文件上传接口
     *
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public synchronized String uploadFile(MultipartFile file) throws Exception {
//        String name = FileUploadUtils.upload(localFilePath, file);
//        try (InputStream in = file.getInputStream(); Ftp ftp = new Ftp("192.168.2.120", 21, "note", "yhl580230");) {
        try (InputStream in = file.getInputStream(); Ftp ftp = new Ftp("192.168.2.120", 21, "anonymous", "", StandardCharsets.UTF_8);) {
            log.info("查看文件列表:{}" , ftp.ls("/"));

            log.info("进来上传了文件:{}" , file.getOriginalFilename());
            LocalDateTime now = LocalDateTime.now();
            String filePath = "/Note/uploadTmp/" + now.getYear() + "/" + NumberUtil.prefix10(now.getMonthValue()) + "/" + NumberUtil.prefix10(now.getDayOfMonth());
            log.info("检测文件夹:{}" , ftp.exist(filePath));
            if (!ftp.exist(filePath)) {
                log.info("创建文件夹:{}" , filePath);
                ftp.mkDirs(filePath);
            }

            String fileName = "" + now.getYear() + (now.getMonthValue() > 10 ? now.getMonthValue() : "0" + now.getMonthValue()) + (now.getDayOfMonth() > 10 ? now.getDayOfMonth() : "0" + now.getDayOfMonth()) + (now.getHour() > 10 ? now.getHour() : "0" + now.getHour()) + (now.getMinute() > 10 ? now.getMinute() : "0" + now.getMinute()) + (now.getSecond() > 10 ? now.getSecond() : "0" + now.getSecond()) + "-" + file.getOriginalFilename();
            ftp.upload(filePath, fileName, in);
            log.info("filePath:{}" , filePath);
            return "/" + "statics" + filePath + "/" + fileName;
        } catch (Exception e) {
            e.printStackTrace();
            log.info("上传了文件:{}" , file.getOriginalFilename());
            throw e;
        }
    }

    /**
     *
     */
    @Override
    public void delete(FileInfo fileInfo) {
        String fileName = fileInfo.getFileName();
        log.info("删除文件：【{}】" , fileName);
//        try (Ftp ftp = new Ftp("192.168.2.120", 21, "note", "yhl580230");) {
        try (Ftp ftp = new Ftp("192.168.2.120" );) {
            log.info("检测文件夹:{}" , ftp.existFile(fileInfo.getFileName()));
            if (ObjectUtil.isNotEmpty(fileName)) {
                String path = fileName.replace(FileFtpConstants.HTTP_DOWN_PREFIX, "" );
                boolean exist = ftp.existFile(path);
                if (exist) {
                    boolean del = ftp.delFile(path);
                    log.info("删除文件：【{}】" , del);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
